package com.wulis.provider.basic.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wulis.common.model.enums.DeletedStateEnum;
import com.wulis.common.support.BaseServiceImpl;
import com.wulis.provider.basic.mapper.PositionMapper;
import com.wulis.provider.basic.model.dto.InsertPositionDto;
import com.wulis.provider.basic.model.dto.QueryPositionPageDto;
import com.wulis.provider.basic.model.dto.UpdatePositionDto;
import com.wulis.provider.basic.model.entity.Position;
import com.wulis.provider.basic.model.vo.QueryPositionListVo;
import com.wulis.provider.basic.model.vo.QueryPositionPageVo;
import com.wulis.provider.basic.model.vo.QueryPositionVo;
import com.wulis.provider.basic.service.PositionService;

import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 * 基础信息管理：职位表 Service实现类
 *
 * @author WuliBao
 * @date 2020-05-21
 */
@Slf4j
@Service
@AllArgsConstructor
public class PositionServiceImpl extends BaseServiceImpl<PositionMapper, Position> implements PositionService {
    private final PositionMapper positionMapper;
    
    /**
     * WEB:查询职位列表(分页)
     *
     * @param queryPositionPageDto queryPositionPageDto
     * @return IPage
     */
    @Override
    public IPage<QueryPositionPageVo> queryPositionPage(@NonNull QueryPositionPageDto queryPositionPageDto) {
        Page<QueryPositionPageVo> page =
            new Page<>(queryPositionPageDto.getPageNumber(), queryPositionPageDto.getPageSize());
        return positionMapper
            .queryPositionPage(page, queryPositionPageDto.getPositionName(), queryPositionPageDto.getEnable());
    }
    
    /**
     * WEB:查询职位列表(全量)
     * 
     * @param enable enable
     * @return List
     */
    @Override
    public List<QueryPositionListVo> queryPositionList(Integer enable) {
        return positionMapper.queryPositionList(enable);
    }
    
    /**
     * WEB:查询职位信息
     *
     * @param id id
     * @return QueryPositionVo
     */
    @Override
    public QueryPositionVo queryPosition(@NonNull Long id) {
        return positionMapper.queryPosition(id);
    }
    
    /**
     * WEB:新增职位信息
     *
     * @param insertPositionDto insertPositionDto
     * @return Integer
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertPosition(@NonNull InsertPositionDto insertPositionDto) {
        // 新增职位
        Position position = new Position();
        BeanUtils.copyProperties(insertPositionDto, position);
        return positionMapper.insert(position);
    }
    
    /**
     * WEB:修改职位信息
     *
     * @param updatePositionDto updatePositionDto
     * @return Integer
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updatePosition(@NonNull UpdatePositionDto updatePositionDto) {
        // 修改职位
        Position position = new Position();
        BeanUtils.copyProperties(updatePositionDto, position);
        return positionMapper.updateById(position);
    }
    
    /**
     * WEB:删除职位信息
     *
     * @param id id
     * @return Integer
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deletePosition(@NonNull Long id) {
        // 删除职位
        Position position = new Position();
        position.setId(id);
        position.setDeleted(DeletedStateEnum.IS_DELETED.value());
        return positionMapper.updateById(position);
    }
}
